//
// Created by pc on 2022/5/23.
//

#include <cstdlib>
#include "Vec4.h"

float Vec4::getV1() const {
    return v1;
}

void Vec4::setV1(float v1) {
    Vec4::v1 = v1;
}

float Vec4::getV2() const {
    return v2;
}

void Vec4::setV2(float v2) {
    Vec4::v2 = v2;
}

float Vec4::getV3() const {
    return v3;
}

void Vec4::setV3(float v3) {
    Vec4::v3 = v3;
}

float Vec4::getV4() const {
    return v4;
}

void Vec4::setV4(float v4) {
    Vec4::v4 = v4;
}

Vec4::Vec4(const float *vecs, int len) {
    if(len != 4) exit(1);
    setV1(vecs[0]);
    setV2(vecs[1]);
    setV3(vecs[2]);
    setV4(vecs[3]);
}

//友元函数可以访问private，参数里的也可以
ostream &operator<<(ostream &os, Vec4 &vec) {
    os << "v1: " << vec.v1 << " | v2: " << vec.v2 << " | v3: " << vec.v3 << " | v4: " << vec.v4;
    return os;
}

istream &operator>>(istream &in, Vec4 &vec) {
    in >> vec.v1 >> vec.v2 >> vec.v3 >> vec.v4;
    return in;
}

/**
 * 可能会有疑问：为啥这里也能像友元函数一样访问other里的私有成员呢？
 * 因为这里并不是类外，这俩都是Vec4，肯定都是自己人，所以随便访问
 */
Vec4 &Vec4::operator+=(Vec4 other) {
    v1 += other.v1;
    v2 += other.v2;
    v3 += other.v3;
    v4 += other.v4;
    other.v1 = 1.111;
    return *this;
}

Vec4 &Vec4::operator-=(const Vec4 &other) {
    v1 -= other.v1;
    v2 -= other.v2;
    v3 -= other.v3;
    v4 -= other.v4;
    return *this;
}

float Vec4::operator*=(const Vec4 &other) {
    float res = 0;
    res = (v1 *= other.v1) + (v2 *= other.v2) + (v3 *= other.v3) + (v4 *= other.v4);
    return res;
}

float Vec4::operator/=(const Vec4 &other) {
    float res = 0;
    res = (v1 /= other.v1) + (v2 /= other.v2) + (v3 /= other.v3) + (v4 /= other.v4);
    return res;
}

Vec4 &Vec4::operator-() {
    v1 = -v1;
    v2 = -v2;
    v3 = -v3;
    v4 = -v4;
    return *this;
}

Vec4::operator float*() const {
    auto *res = new float[4];
    res[0] = v1;
    res[1] = v2;
    res[2] = v3;
    res[3] = v4;
    return res;
}

float& Vec4::operator[](int index) {
    auto* res = (float*)this;
    return res[index];
}

//Vec4 &Vec4::operator+=(const Complex &other) {
//    this->v1 += other.imagine;
//}

Vec4 operator+(const Vec4 &v1, const Vec4 &v2){
    Vec4 res = v1;
    res += v2;
    return res;
}


Vec4 operator-(const Vec4 &v1, const Vec4 &v2){
    Vec4 res = v1;
    res -= v2;
    return res;
}


float operator*(const Vec4 &v1, const Vec4 &v2){
    Vec4 temp = v1;
    float res = temp *= v2;
    return res;
}

float operator/(Vec4 &v1, const Vec4 &v2){
    Vec4 temp = v1;
    float res = temp /= v2;
    return res;
}